from time import sleep
from random import randint
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
# 打开登录页面
driver.get('http://localhost/JXC/index.asp')

# 完成登录
ipts = driver.find_elements_by_tag_name('input')
ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
ipts[2].click()

sleep(2)
# 从当前默认位置切换到菜单框架中
driver.switch_to.frame('left')

# 点击菜单 系统设置
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()

sleep(2)
# 继续点击：计量单位管理
danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
driver.find_element_by_css_selector(danwei_css).click()
sleep(1)

# 切换原始位置
driver.switch_to.default_content()
# 切到右侧框架
driver.switch_to.frame('right')
sleep(2)

#点击全选
driver.find_element_by_id('chkall').click()
sleep(1)

#点击删除
del_css='body > table > tbody > tr:nth-child(2) > td:nth-child(2) > form > table > tbody > tr:nth-child(3) > td > table > tbody > tr > td:nth-child(2) > input.button'
driver.find_element_by_css_selector(del_css).click()
sleep(1)

#处理弹窗
driver.switch_to.alert.accept()
sleep(1)

# 点击添加单位按钮
bt_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()
sleep(2)

# 输入单位名称
driver.find_element_by_name('danwei').send_keys('哈哈哈')
# 点击确认按钮
driver.find_element_by_name('submit').click()
sleep(2)
# 处理弹框
driver.switch_to.alert.accept()
sleep(2)

# 切换原始位置
driver.switch_to.default_content()
# 从当前默认位置切换到菜单框架中
driver.switch_to.frame('left')

# 点击菜单 系统设置
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()
sleep(2)
# 继续点击：产品大类管理
dalei_css = '#g_6 > table > tbody > tr:nth-child(5) > td > table > tbody > tr > td:nth-child(2)'
driver.find_element_by_css_selector(dalei_css).click()
sleep(1)

# 切换原始位置
driver.switch_to.default_content()
# 切到右侧框架
driver.switch_to.frame('right')
sleep(1)
# 点击添加大类按钮
dlbt_css = 'body > table > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td:nth-child(2) > input'
driver.find_element_by_css_selector(dlbt_css).click()
sleep(2)
#添加一个大类
driver.find_element_by_name('bigclass').send_keys('手机')
sleep(1)
#点击确认
driver.find_element_by_name('submit').click()
sleep(1)
# 处理弹框
driver.switch_to.alert.accept()
sleep(1)

# 切换原始位置
driver.switch_to.default_content()
# 从当前默认位置切换到菜单框架中
driver.switch_to.frame('left')
# 点击菜单 系统设置
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()
sleep(1)
# 继续点击：产品小类管理
xiaolei_css = '#g_6 > table > tbody > tr:nth-child(6) > td > table > tbody > tr > td:nth-child(2)'
driver.find_element_by_css_selector(xiaolei_css).click()
sleep(1)
# 切换原始位置
driver.switch_to.default_content()
# 切到右侧框架
driver.switch_to.frame('right')
sleep(1)



#点击添加小类按钮
xlbt_css = 'body > table:nth-child(2) > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td:nth-child(2) > input'
driver.find_element_by_css_selector(xlbt_css).click()
sleep(2)
# 获取列表项的数量
lstnum=Select(driver.find_element_by_name('bigclass'))
lst_count = len(lstnum.options)

# 根据数量，生成一个随机数, 参数:闭区间的范围
# 列表项数量：3， 序号范围：[0,2]
# 随机生成一个列表项的序号[0,2]
rand = randint(0, lst_count-1)

# 通过随机产生的序号，选择一个列表项
lstnum.select_by_index(rand)
sleep(1)
#添加一个小类
driver.find_element_by_name('smallclass').send_keys('华为')
sleep(1)
#点击确认
driver.find_element_by_name('submit').click()
sleep(1)
# 处理弹框
driver.switch_to.alert.accept()
sleep(1)

# 切换到顶部框架
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)
# 关闭浏览器
driver.quit()





